home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #5 / Amiga Plus CD - 2000 - No. 5.iso / Tools / Dev / FPSE_src / idctfst.c < prev    next >
Text File  |  2000-01-01  |  10KB  |  286 lines

  1. /*
  2.  * jidctfst.c
  3.  *
  4.  * Copyright (C) 1994-1996, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains a fast, not so accurate integer implementation of the
  9.  * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
  10.  * must also perform dequantization of the input coefficients.
  11.  *
  12.  * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
  13.  * on each row (or vice versa, but it's more convenient to emit a row at
  14.  * a time).  Direct algorithms are also available, but they are much more
  15.  * complex and seem not to be any faster when reduced to code.
  16.  *
  17.  * This implementation is based on Arai, Agui, and Nakajima's algorithm for
  18.  * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
  19.  * Japanese, but the algorithm is described in the Pennebaker & Mitchell
  20.  * JPEG textbook (see REFERENCES section in file README).  The following code
  21.  * is based directly on figure 4-8 in P&M.
  22.  * While an 8-point DCT cannot be done in less than 11 multiplies, it is
  23.  * possible to arrange the computation so that many of the multiplies are
  24.  * simple scalings of the final outputs.  These multiplies can then be
  25.  * folded into the multiplications or divisions by the JPEG quantization
  26.  * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
  27.  * to be done in the DCT itself.
  28.  * The primary disadvantage of this method is that with fixed-point math,
  29.  * accuracy is lost due to imprecise representation of the scaled
  30.  * quantization values.  The smaller the quantization table entry, the less
  31.  * precise the scaled value, so this implementation does worse with high-
  32.  * quality-setting files than with low-quality ones.
  33.  */
  34.  
  35. /*
  36.  * This module is specialized to the case DCTSIZE = 8.
  37.  */
  38.  
  39. /* Scaling decisions are generally the same as in the LL&M algorithm;
  40.  * see jidctint.c for more details.  However, we choose to descale
  41.  * (right shift) multiplication products as soon as they are formed,
  42.  * rather than carrying additional fractional bits into subsequent additions.
  43.  * This compromises accuracy slightly, but it lets us save a few shifts.
  44.  * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
  45.  * everywhere except in the multiplications proper; this saves a good deal
  46.  * of work on 16-bit-int machines.
  47.  *
  48.  * The dequantized coefficients are not integers because the AA&N scaling
  49.  * factors have been incorporated.  We represent them scaled up by PASS1_BITS,
  50.  * so that the first and second IDCT rounds have the same input scaling.
  51.  * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
  52.  * avoid a descaling shift; this compromises accuracy rather drastically
  53.  * for small quantization table entries, but it saves a lot of shifts.
  54.  * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
  55.  * so we use a much larger scaling factor to preserve accuracy.
  56.  *
  57.  * A final compromise is to represent the multiplicative constants to only
  58.  * 8 fractional bits, rather than 13.  This saves some shifting work on some
  59.  * machines, and may also reduce the cost of multiplication (since there
  60.  * are fewer one-bits in the constants).
  61.  */
  62.  
  63. #define   BITS_IN_JSAMPLE     8
  64.  
  65. #if BITS_IN_JSAMPLE == 8
  66. #define CONST_BITS  8
  67. #define PASS1_BITS  2
  68. #else
  69. #define CONST_BITS  8
  70. #define PASS1_BITS  1         /* lose a little precision to avoid overflow */
  71. #endif
  72.  
  73. /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
  74.  * causing a lot of useless floating-point operations at run time.
  75.  * To get around this we use the following pre-calculated constants.
  76.  * If you change CONST_BITS you may want to add appropriate values.
  77.  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
  78.  */
  79.  
  80. #if CONST_BITS == 8
  81. #define FIX_1_082392200  (277)          /* FIX(1.082392200) */
  82. #define FIX_1_414213562  (362)          /* FIX(1.414213562) */
  83. #define FIX_1_847759065  (473)          /* FIX(1.847759065) */
  84. #define FIX_2_613125930  (669)          /* FIX(2.613125930) */
  85. #else
  86. #define FIX_1_082392200  FIX(1.082392200)
  87. #define FIX_1_414213562  FIX(1.414213562)
  88. #define FIX_1_847759065  FIX(1.847759065)
  89. #define FIX_2_613125930  FIX(2.613125930)
  90. #endif
  91.  
  92.  
  93. /* We can gain a little more speed, with a further compromise in accuracy,
  94.  * by omitting the addition in a descaling shift.  This yields an incorrectly
  95.  * rounded result half the time...
  96.  */
  97.  
  98.  
  99. /* Multiply a DCTELEM variable by an INT32 constant, and immediately
  100.  * descale to yield a DCTELEM result.
  101.  */
  102.  
  103. #define MULTIPLY(var,const)  (DESCALE((var) * (const), CONST_BITS))
  104.  
  105.  
  106. /* Dequantize a coefficient by multiplying it by the multiplier-table
  107.  * entry; produce a DCTELEM result.  For 8-bit data a 16x16->16
  108.  * multiplication will do.  For 12-bit data, the multiplier table is
  109.  * declared INT32, so a 32-bit multiply will be used.
  110.  */
  111.  
  112. #if BITS_IN_JSAMPLE == 8
  113. #define DEQUANTIZE(coef,quantval)  (coef)
  114. #else
  115. #define DEQUANTIZE(coef,quantval)  \
  116.      DESCALE((coef), IFAST_SCALE_BITS-PASS1_BITS)
  117. #endif
  118.  
  119.  
  120. /* Like DESCALE, but applies to a DCTELEM and produces an int.
  121.  * We assume that int right shift is unsigned if INT32 right shift is.
  122.  */
  123.  
  124. #define DESCALE(x,n)  ((x)>>(n))
  125. #define   RANGE(n)  (n)
  126. #define   BLOCK     int
  127.  
  128. /*
  129.  * Perform dequantization and inverse DCT on one block of coefficients.
  130.  */
  131. #define   DCTSIZE   8
  132. #define   DCTSIZE2  64
  133.  
  134. static void idct1(BLOCK *block)
  135. {
  136.      int val = RANGE(DESCALE(block[0], PASS1_BITS+3));
  137.      int i;
  138.      for(i=0;i<DCTSIZE2;i++) block[i]=val;
  139. }
  140.  
  141. void idct(BLOCK *block,int k)
  142. {
  143.   int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  144.   int z5, z10, z11, z12, z13;
  145.   BLOCK *ptr;
  146.   int i;
  147.  
  148.   /* Pass 1: process columns from input, store into work array. */
  149.   switch(k){
  150.   case 1:idct1(block); return;
  151.   }
  152.  
  153.   ptr = block;
  154.   for (i = 0; i< DCTSIZE; i++,ptr++) {
  155.     /* Due to quantization, we will usually find that many of the input
  156.      * coefficients are zero, especially the AC terms.  We can exploit this
  157.      * by short-circuiting the IDCT calculation for any column in which all
  158.      * the AC terms are zero.  In that case each output is equal to the
  159.      * DC coefficient (with scale factor as needed).
  160.      * With typical images and quantization tables, half or more of the
  161.      * column DCT calculations can be simplified this way.
  162.      */
  163.     
  164.     if ((ptr[DCTSIZE*1] | ptr[DCTSIZE*2] | ptr[DCTSIZE*3] |
  165.       ptr[DCTSIZE*4] | ptr[DCTSIZE*5] | ptr[DCTSIZE*6] |
  166.       ptr[DCTSIZE*7]) == 0) {
  167.       /* AC terms all zero */
  168.       ptr[DCTSIZE*0] = 
  169.       ptr[DCTSIZE*1] = 
  170.       ptr[DCTSIZE*2] = 
  171.       ptr[DCTSIZE*3] = 
  172.       ptr[DCTSIZE*4] = 
  173.       ptr[DCTSIZE*5] = 
  174.       ptr[DCTSIZE*6] = 
  175.       ptr[DCTSIZE*7] = 
  176.           ptr[DCTSIZE*0];
  177.       
  178.       continue;
  179.     }
  180.     
  181.     /* Even part */
  182.  
  183.     z10 = ptr[DCTSIZE*0] + ptr[DCTSIZE*4];   /* phase 3 */
  184.     z11 = ptr[DCTSIZE*0] - ptr[DCTSIZE*4];
  185.     z13 = ptr[DCTSIZE*2] + ptr[DCTSIZE*6];   /* phases 5-3 */
  186.     z12 = MULTIPLY(ptr[DCTSIZE*2] - ptr[DCTSIZE*6], FIX_1_414213562) - z13; /* 2*c4 */
  187.  
  188.     tmp0 = z10 + z13;    /* phase 2 */
  189.     tmp3 = z10 - z13;
  190.     tmp1 = z11 + z12;
  191.     tmp2 = z11 - z12;
  192.     
  193.     /* Odd part */
  194.  
  195.     z13 = ptr[DCTSIZE*3] + ptr[DCTSIZE*5];        /* phase 6 */
  196.     z10 = ptr[DCTSIZE*3] - ptr[DCTSIZE*5];
  197.     z11 = ptr[DCTSIZE*1] + ptr[DCTSIZE*7];
  198.     z12 = ptr[DCTSIZE*1] - ptr[DCTSIZE*7];
  199.  
  200.     z5 = MULTIPLY(z12 - z10, FIX_1_847759065);
  201.     tmp7 = z11 + z13;         /* phase 5 */
  202.     tmp6 = MULTIPLY(z10, FIX_2_613125930) + z5 - tmp7; /* phase 2 */
  203.     tmp5 = MULTIPLY(z11 - z13, FIX_1_414213562) - tmp6;
  204.     tmp4 = MULTIPLY(z12, FIX_1_082392200) - z5 + tmp5;
  205.  
  206.     ptr[DCTSIZE*0] = (tmp0 + tmp7);
  207.     ptr[DCTSIZE*7] = (tmp0 - tmp7);
  208.     ptr[DCTSIZE*1] = (tmp1 + tmp6);
  209.     ptr[DCTSIZE*6] = (tmp1 - tmp6);
  210.     ptr[DCTSIZE*2] = (tmp2 + tmp5);
  211.     ptr[DCTSIZE*5] = (tmp2 - tmp5);
  212.     ptr[DCTSIZE*4] = (tmp3 + tmp4);
  213.     ptr[DCTSIZE*3] = (tmp3 - tmp4);
  214.  
  215.   }
  216.   
  217.   /* Pass 2: process rows from work array, store into output array. */
  218.   /* Note that we must descale the results by a factor of 8 == 2**3, */
  219.   /* and also undo the PASS1_BITS scaling. */
  220.  
  221.   ptr = block;
  222.   for (i = 0; i < DCTSIZE; i++ ,ptr+=DCTSIZE) {
  223.     /* Rows of zeroes can be exploited in the same way as we did with columns.
  224.      * However, the column calculation has created many nonzero AC terms, so
  225.      * the simplification applies less often (typically 5% to 10% of the time).
  226.      * On machines with very fast multiplication, it's possible that the
  227.      * test takes more time than it's worth.  In that case this section
  228.      * may be commented out.
  229.      */
  230.     
  231. #ifndef NO_ZERO_ROW_TEST
  232.     if ((ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5] | ptr[6] |
  233.       ptr[7]) == 0) {
  234.       /* AC terms all zero */
  235.       ptr[0] = 
  236.       ptr[1] = 
  237.       ptr[2] = 
  238.       ptr[3] = 
  239.       ptr[4] = 
  240.       ptr[5] = 
  241.       ptr[6] = 
  242.       ptr[7] = 
  243.           RANGE(DESCALE(ptr[0], PASS1_BITS+3));;
  244.  
  245.       continue;
  246.     }
  247. #endif
  248.     
  249.     /* Even part */
  250.  
  251.     z10 = ptr[0] + ptr[4];
  252.     z11 = ptr[0] - ptr[4];
  253.     z13 = ptr[2] + ptr[6];
  254.     z12 = MULTIPLY(ptr[2] - ptr[6], FIX_1_414213562) - z13;
  255.  
  256.     tmp0 = z10 + z13;
  257.     tmp3 = z10 - z13;
  258.     tmp1 = z11 + z12;
  259.     tmp2 = z11 - z12;
  260.  
  261.     /* Odd part */
  262.  
  263.     z13 = ptr[3] + ptr[5];
  264.     z10 = ptr[3] - ptr[5];
  265.     z11 = ptr[1] + ptr[7];
  266.     z12 = ptr[1] - ptr[7];
  267.  
  268.     z5 = MULTIPLY(z12 - z10, FIX_1_847759065);
  269.     tmp7 = z11 + z13;         /* phase 5 */
  270.     tmp6 = MULTIPLY(z10, FIX_2_613125930) + z5 - tmp7; /* phase 2 */
  271.     tmp5 = MULTIPLY(z11 - z13, FIX_1_414213562) - tmp6;
  272.     tmp4 = MULTIPLY(z12, FIX_1_082392200) - z5 + tmp5;
  273.  
  274.     /* Final output stage: scale down by a factor of 8 and range-limit */
  275.  
  276.     ptr[0] = RANGE(DESCALE(tmp0 + tmp7, PASS1_BITS+3));;
  277.     ptr[7] = RANGE(DESCALE(tmp0 - tmp7, PASS1_BITS+3));;
  278.     ptr[1] = RANGE(DESCALE(tmp1 + tmp6, PASS1_BITS+3));;
  279.     ptr[6] = RANGE(DESCALE(tmp1 - tmp6, PASS1_BITS+3));;
  280.     ptr[2] = RANGE(DESCALE(tmp2 + tmp5, PASS1_BITS+3));;
  281.     ptr[5] = RANGE(DESCALE(tmp2 - tmp5, PASS1_BITS+3));;
  282.     ptr[4] = RANGE(DESCALE(tmp3 + tmp4, PASS1_BITS+3));;
  283.     ptr[3] = RANGE(DESCALE(tmp3 - tmp4, PASS1_BITS+3));;
  284.  
  285.   }
  286. }